/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * Description: This activity displays a larger version of the cover of the currently selected item. 
 */

package us.in.lib.acpl.Mobile_ACPL;

import java.net.URL;

import com.google.analytics.tracking.android.EasyTracker;

import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class LargeCoverActivity extends Activity implements OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.large_cover);
		String isbn = getIntent().getStringExtra("isbn");
		findViewById(R.id.largeCoverLayout).setOnClickListener(this);
		new GetImageTask().execute(isbn);

	}
	  @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance().activityStart(this); // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance().activityStop(this); // Add this method.
	  }

	public void setImage(Bitmap image) {
		findViewById(R.id.largeCoverBar).setVisibility(View.GONE);
		ImageView iv = (ImageView) this.findViewById(R.id.largeCover);
		iv.setImageBitmap(image);
	}

	private class GetImageTask extends AsyncTask<String, Void, Bitmap> {

		@Override
		protected Bitmap doInBackground(String... params) {
			String isbn = params[0];
			Bitmap coverImage = null;

			try {
				URL newurl = new URL(SearchResultsTitleDetailActivity.URL_START
						+ isbn
						+ SearchResultsTitleDetailActivity.IMAGE_LARGE_URL_END);
				coverImage = BitmapFactory.decodeStream(newurl.openConnection()
						.getInputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return coverImage;
		}

		@Override
		protected void onPostExecute(Bitmap image) {
			setImage(image);
		}

	}

	@Override
	public void onClick(View arg0) {
		finish();

	}


}
